<template>
  <div id="app">
    <h2>购物车</h2>
    <div class="cart-item" v-for="item in cartItems" :key="item.id">
      <span>{{ item.name }} - ¥{{ item.price }} - 数量: {{ item.quantity }}</span>
      <div class="quantity-controls">
        <button @click="increment(item.id)">+</button>
        <button @click="decrement(item.id)">-</button>
      </div>
    </div>
    <div class="total">
      总价: ¥{{ totalPrice }}
    </div>
  </div>
</template>

<script>
export default {
  computed: {
    cartItems() {
      return this.$store.state.cartItems
    },
    totalPrice() {
      return this.$store.getters.totalPrice
    }
  },
  methods: {
    increment(itemId) {
      this.$store.commit('INCREMENT_QUANTITY', itemId)
    },
    decrement(itemId) {
      this.$store.commit('DECREMENT_QUANTITY', itemId)
    }
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  max-width: 500px;
  margin: 20px auto;
  padding: 20px;
  border: 1px solid #eee;
  border-radius: 5px;
}

h2 {
  text-align: center;
  color: #333;
}

.cart-item {
  padding: 10px;
  margin: 10px 0;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.quantity-controls {
  display: flex;
  gap: 5px;
}

button {
  background-color: #42b983;
  color: white;
  border: none;
  padding: 5px 10px;
  border-radius: 3px;
  cursor: pointer;
  min-width: 30px;
}

button:hover {
  background-color: #3aa876;
}

.total {
  margin-top: 20px;
  padding-top: 10px;
  border-top: 1px solid #eee;
  font-weight: bold;
  text-align: right;
  font-size: 1.1em;
}
</style>